package com.yuan.config;

import org.springframework.context.annotation.FilterType;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;

/**
 *
 * @ComponentScan.Filter 的type属性自定义规则类(type = FilterType.CUSTOM)，实现了TypeFilter接口
 */
public class MyTypeFilter implements TypeFilter {
    /**
     *
     * @param metadataReader the metadata reader for the target class (读取到的正在扫描的类的信息)
     * @param metadataReaderFactory  a factory for obtaining metadata readers (是一个工厂,可以获取其他任何类的信息)
     * 	* for other classes (such as superclasses and interfaces)
     * @return
     * @throws IOException
     */
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        //获取当前类注解信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        //获取当前正在扫描的类的信息，比它的类型式什么,实现了哪些接口等等
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        //获取当前类的类名
        String className = classMetadata.getClassName();
        System.out.println("---className---->>>>"+className);
        //获取当前类的资源信息(如类的路径)
        Resource resource = metadataReader.getResource();
        //如果类名中包含er则匹配成功
        if(className.contains("er")){
            return true;
        }

        return false;
    }
}
